package cn.zjj.product.service.impl;

import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.pet.domain.SearchMasterMsgAuditLog;
import cn.zjj.product.domain.Product;
import cn.zjj.product.mapper.ProductDetailMapper;
import cn.zjj.product.mapper.ProductMapper;
import cn.zjj.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;


    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;

    @Override
    public void insert(Product product) {
        aduit(product);
        productMapper.insert(product);
        product.getProductDetail().setProduct_id(product.getId());
        productDetailMapper.insert(product.getProductDetail());
    }

    private void aduit(Product product){
        //文字审核
        Boolean textBoolean = baiduAiAuditService.textAudit(product.getName());
        if(!textBoolean){
            throw new BusinessException("商品名不合法,审核未通过");
        }
        String resources = product.getResources();

        String[] imgs = resources.split(",");
        for (String img : imgs) {

            Boolean aBoolean = baiduAiAuditService.imageAudit("http://123.207.27.208"+img);
            if(!aBoolean){
                throw new BusinessException("图片内容不合法,审核未通过");
            }
        }
    }


    @Override
    public void update(Product product) {
        aduit(product);
        productMapper.update(product);
        productDetailMapper.update(product.getProductDetail());
    }

    @Override
    public void delete(Long id) {
        productDetailMapper.delete(id);

        productMapper.delete(id);
    }


    //上架
    @Override
    public void onsale(List<Long> ids){
        //上架之前应该还有审核程序

        Date onsaletime = new Date();
        productMapper.onsale(ids,onsaletime);
    }

    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();
        productMapper.offsale(ids,offsaletime);
    }

}
